public class Num516 {
    public int longestPalindromeSubseq(String s) {
        int n = s.length();
        char[] chars = s.toCharArray();
        int[][] dp = new int[n][n];
        for (int len = 1; len <= n; len++) {
            for (int l = 0; l + len - 1 < n; l++) {
                int r = l + len - 1;
                if (len == 1) {
                    dp[l][r] = 1;
                }else if (len == 2) {
                    dp[l][r] = chars[l] == chars[r] ? 2 : 1;
                }else {
                    dp[l][r] = Math.max(dp[l +1][r],dp[l][r - 1]);
                    dp[l][r] = Math.max(dp[l][r],dp[l + 1][r - 1] + (chars[l] == chars[r] ? 2 : 0));
                }
            }
        }
        return dp[0][n - 1];
    }
}
